package com.ruoyi.wisdom.event;

import com.ruoyi.wisdom.domain.IotPickGroup;
import com.ruoyi.wisdom.domain.swap.SwapPickGroup;
import com.ruoyi.wisdom.service.IIotPickGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PickGroupEvent extends IotEvent {

    @Autowired
    private IIotPickGroupService iotPickGroupService;

    /**
     * 处理
     *
     * @param swapPickGroup
     */
    public void handle(SwapPickGroup swapPickGroup) {
        if (swapPickGroup.getOperate() == null) {
            save(swapPickGroup);
        } else {
            switch (swapPickGroup.getOperate()) {
                case "delete":
                case "remove":
                    delete(swapPickGroup);
                    break;
                default:
                    save(swapPickGroup);
            }
        }
    }

    /**
     * 添加|编辑
     *
     * @param swapPickGroup
     */
    public void save(SwapPickGroup swapPickGroup) {
        IotPickGroup result = iotPickGroupService.selectIotPickGroupById(swapPickGroup.getId());
        // 实体装换
        IotPickGroup iotPickGroup = getModelMapper().map(swapPickGroup, IotPickGroup.class);
        if (result != null) {
            iotPickGroup.setId(swapPickGroup.getId());
            iotPickGroupService.updateIotPickGroup(iotPickGroup);
        } else {
            iotPickGroupService.insertIotPickGroup(iotPickGroup);
        }
    }

    /**
     * 删除
     *
     * @param swapPickGroup
     */
    public void delete(SwapPickGroup swapPickGroup) {
        iotPickGroupService.deleteIotPickGroupById(swapPickGroup.getId());
    }
}
